package com.springboot.controller;

import com.springboot.common.Result;
import com.springboot.entity.Order;
import com.springboot.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "用户订单管理",value = "订单管理")
@RestController
@RequestMapping("/shop")
public class OrderController {
    @Autowired
    OrderService orderService;

    /**
     * 查询指定用户订单;
     * @param userId 根据用户编号查看用户订单详情;
     * @return  返回用户订单详情;
     */
    @GetMapping("/searchUserOrder")
    @ApiOperation(value = "用户订单详情",httpMethod = "GET")
    public Result SearchUserItem(@RequestParam(value = "userId") Integer userId){
        Order  orderlist=orderService.SearchOrder(userId);
        if(orderlist!=null){
            return Result.success("查询订单成功",orderlist);
        }
        return Result.fail("该用户没有订单",null);
    }


    /**
     * 修改用户订单状态;
     * @param status  订单状态
     * @return
     */
    @PostMapping("/updateUserOrder")
    @ApiOperation(value = "更新用户订单详状态",httpMethod = "POST")
    public Result UpdateUserOrder(
            @RequestParam("status")Integer status,
            @RequestParam("userId") Integer userId
    ){
        Integer i= orderService.UpdateUserOrderStatus(status,userId);
        if(i!=0){
            return Result.success("修改成功",i);
        }
        return Result.fail("修改失败",null);
    }


    /**
     * 查询所有用户的订单;
     * @return  返回所有订单;
     */
    @GetMapping("/AllUserOrder")
    @ApiOperation(value = "查询所有用户的订单",httpMethod = "GET")
    public Result SearchAllUserOrder(){
        List<Order> orders= orderService.SearchAllUserOrder();
        return Result.success("所有订单如下所示",orders);
    }


}
